/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package xmlutils;

import DTO.album.AlbumType;
import DTO.album.Albums;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;

/**
 *
 * @author DinhNP
 */
public class AlbumsSearching {
    private final String AlbumXML = "/xml/Albums.xml";

    public Albums searchAlbumsHot(String realPath){
        Albums albums = null;


        try{
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sax = spf.newSAXParser();
            SearchHotAlbumsSAXHandler objHandler = new SearchHotAlbumsSAXHandler();
            File file = new File(realPath+AlbumXML);
            sax.parse(file, objHandler);

            albums = objHandler.getAlbumList();

            return albums;

        }catch (IOException ex) {
            Logger.getLogger(AlbumsSearching.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }catch (ParserConfigurationException ex) {
            Logger.getLogger(AlbumsSearching.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        } catch (SAXException ex) {
            Logger.getLogger(AlbumsSearching.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

//    public Albums searchLastestAlbum(String realPath){
//        Albums albums = null;
//
//
//        try{
//            SAXParserFactory spf = SAXParserFactory.newInstance();
//            SAXParser sax = spf.newSAXParser();
//            HotAlbumsSAXHandler objHandler = new HotAlbumsSAXHandler();
//            File file = new File(realPath+AlbumXML);
//            sax.parse(file, objHandler);
//
//            albums = objHandler.getAlbumList();
//
//            return albums;
//
//        }catch (IOException ex) {
//            Logger.getLogger(AlbumsSearching.class.getName()).log(Level.SEVERE, null, ex);
//            return null;
//        }catch (ParserConfigurationException ex) {
//            Logger.getLogger(AlbumsSearching.class.getName()).log(Level.SEVERE, null, ex);
//            return null;
//        } catch (SAXException ex) {
//            Logger.getLogger(AlbumsSearching.class.getName()).log(Level.SEVERE, null, ex);
//            return null;
//        }
//    }

    public Albums selectTopAlbums(Albums albums){

        Albums topAlbums = new Albums();
        for (int i=0;i<albums.getAlbum().size();i++){
            int temp =albums.getAlbum().get(i).getAlbumViews().intValue();
            if (temp>albums.getAlbum().get(i+1).getAlbumViews().intValue()){
                temp = albums.getAlbum().get(i+1).getAlbumViews().intValue();
                albums.getAlbum().get(i+1).setAlbumViews(albums.getAlbum().get(i).getAlbumViews());
                albums.getAlbum().get(i).setAlbumViews(BigInteger.valueOf(temp));
            }
        }
        if (albums.getAlbum().size()>8){
            for (int i = 0; i<8;i++){
                topAlbums.getAlbum().set(i, albums.getAlbum().get(albums.getAlbum().size()-1-i));
            }
            return topAlbums;
        }else return albums;
    }
}
